﻿using Orleans;
using Orleans.Runtime;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace IOP.Orleans
{
    /// <summary>
    /// 观察者管理器接口
    /// </summary>
    /// <typeparam name="TObserver"></typeparam>
    public interface IObserverManager<TObserver> : IEnumerable<TObserver>
        where TObserver : IAddressable
    {
        /// <summary>
        /// 获取当前时间
        /// </summary>
        Func<DateTime> GetDateTime { get; set; }
        /// <summary>
        /// 订阅者数量
        /// </summary>
        int Count { get; }
        /// <summary>
        /// 订阅者
        /// </summary>
        IDictionary<IAddressable, TObserver> Observers { get; }
        /// <summary>
        /// 移除所有观察者
        /// </summary>
        void Clear();
        /// <summary>
        /// 判断一个监听者是否已经订阅了消息
        /// </summary>
        /// <param name="observer"></param>
        /// <returns></returns>
        bool IsSubscribed(TObserver observer);
        /// <summary>
        /// 判断一个监听者是否已经订阅了消息
        /// </summary>
        /// <param name="observer"></param>
        /// <returns></returns>
        bool IsSubscribed(IGrainObserver observer);
        /// <summary>
        /// 订阅
        /// </summary>
        /// <param name="observer"></param>
        void Subscribe(TObserver observer);
        /// <summary>
        /// 取消订阅
        /// </summary>
        /// <param name="subscriber"></param>
        void Unsubscribe(TObserver subscriber);
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        Task Notify(Func<TObserver, Task> notification, IEnumerable<TObserver> targetObserver = null);
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="predicate"></param>
        void Notify(Action<TObserver> notification, IEnumerable<TObserver> targetObserver = null);
    }
}
